/* Yet Another Experimental Software Renderer
 *
 * Copyright (c) 2010 Pawel "Hery" Dziepak
 */

#include "tessellator.h"
#include "interpolate.h"

using namespace yaesr;

void default_ts::tessellate(triangle x, std::vector<triangle> &tri_list, int level) {
	if (level == 0) {
		tri_list.push_back(x);
		return;
	}

	vertex mid01 = interpolate::interpolate_line(x.vert0, x.vert1, 0.5);
	vertex mid12 = interpolate::interpolate_line(x.vert1, x.vert2, 0.5);
	vertex mid02 = interpolate::interpolate_line(x.vert0, x.vert2, 0.5);

	triangle temp;

	// 1 01 12
	temp.vert0 = x.vert1;
	temp.vert1 = mid01;
	temp.vert2 = mid12;
	tessellate(temp, tri_list, level - 1);

	// 01 02 12
	temp.vert0 = mid01;
	temp.vert1 = mid02;
	temp.vert2 = mid12;
	tessellate(temp, tri_list, level - 1);

	// 12 02 2
	temp.vert0 = mid12;
	temp.vert1 = mid02;
	temp.vert2 = x.vert2;
	tessellate(temp, tri_list, level - 1);

	// 01 02 0
	temp.vert0 = mid01;
	temp.vert1 = mid02;
	temp.vert2 = x.vert0;
	tessellate(temp, tri_list, level - 1);
}
